package no.saiboten.milkywave.controller;

import no.saiboten.milkywave.component.WaterLevelEntity;
import no.saiboten.milkywave.service.MilkyWaveHttpService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WaterLevelController {

	WaterLevelEntity waterLevel;

	MilkyWaveHttpService service;

	@Autowired
	public WaterLevelController(WaterLevelEntity waterLevel,
			MilkyWaveHttpService service) {
		this.waterLevel = waterLevel;
		this.service = service;
	}

	@RequestMapping("/waterLevelHtml")
	public ModelAndView getCurrentWaterLevelHtml() {

		ModelAndView mav = new ModelAndView("waterlevelhtml");
		mav.addObject("waterLevel", waterLevel.getCurrentWaterLevel());
		return mav;
	}

	@RequestMapping("/waterLevel")
	public ModelAndView getCurrentWaterLevel() {

		ModelAndView mav = new ModelAndView("waterlevelplain");
		mav.addObject("waterLevel", waterLevel.getCurrentWaterLevel());
		return mav;
	}

	@RequestMapping("/updateWaterLevel")
	public ModelAndView updateWaterLevel() {
		Double value = service.findLatestValue();
		if (value != -1) {
			waterLevel.setCurrentWaterLevel(value);
		}
		ModelAndView mav = new ModelAndView("waterlevelplain");
		mav.addObject("waterLevel", waterLevel.getCurrentWaterLevel());
		return mav;
	}

}
